package cn.whale.config;

import cn.hutool.core.collection.CollectionUtil;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

class PersistentChatMemoryStore implements ChatMemoryStore {

    private final ConcurrentHashMap<Object, List<ChatMessage>> messageStore = new ConcurrentHashMap<>(16);

    @Override
    public List<ChatMessage> getMessages(Object memoryId) {
        System.out.println(memoryId);
        List<ChatMessage> chatMessages = messageStore.get(memoryId);
        if(CollectionUtil.isEmpty(chatMessages)){
            return CollectionUtil.newArrayList();
        }
        return chatMessages;
    }

    @Override
    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
        messageStore.put(memoryId, messages);
    }

    @Override
    public void deleteMessages(Object memoryId) {
        messageStore.remove(memoryId);
    }
}